require('dotenv').config();
const mongoose = require('mongoose');
const User = require('../models/user.model');

const initDB = async () => {
    try {
        // 连接数据库
        await mongoose.connect(process.env.MONGODB_URI);
        console.log('Connected to MongoDB');

        // 清空现有数据
        await User.deleteMany({});
        console.log('Cleared existing data');

        // 创建示例用户
        const users = [
            {
                name: '测试用户1',
                email: 'test1@example.com',
                password: '$2a$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewdBPj2NXFAZdzye', // 123456
                age: 25
            },
            {
                name: '测试用户2',
                email: 'test2@example.com',
                password: '$2a$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewdBPj2NXFAZdzye', // 123456
                age: 30
            }
        ];

        await User.insertMany(users);
        console.log('Created sample users');

        console.log('Database initialization completed');
        process.exit(0);
    } catch (error) {
        console.error('Error initializing database:', error);
        process.exit(1);
    }
};

initDB(); 